﻿package com.sxhalo.manage.web.action;

import java.util.*;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.sxhalo.manage.map.SysModules;
import com.sxhalo.manage.map.SysOperation;
import com.sxhalo.manage.map.SysRoleModules;
import com.sxhalo.manage.map.SysRoles;
import com.sxhalo.manage.map.SysUsers;
import com.sxhalo.manage.service.IBaseService;
import com.sxhalo.manage.utils.Pager;
import com.sxhalo.manage.web.form.OperationForm;

public class OperationAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private int draw = 1;

	private int start = 0;

	private int length = 20;

	private OperationForm operation;

	// 将会被Struts2序列化为JSON字符串的对象
	private Map<String, Object> dataMap;
    
    private List dataList;

	// 业务层
	private IBaseService<SysOperation> myService;
	
	private IBaseService<SysUsers> userService;
	
	private IBaseService<SysRoleModules> roleService;

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

    public OperationForm getOperation() {
		return operation;
	}

	public void setOperation(OperationForm operation) {
		this.operation = operation;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

    public List<OperationForm> getDataList() {
		return dataList;
	}

	public void setDataList(List<OperationForm> dataList) {
		this.dataList = dataList;
	}
    
	public IBaseService<SysOperation> getMyService() {
		return myService;
	}

	public void setMyService(IBaseService<SysOperation> myService) {
		this.myService = myService;
	}

	public IBaseService<SysUsers> getUserService() {
		return userService;
	}

	public void setUserService(IBaseService<SysUsers> userService) {
		this.userService = userService;
	}

	public IBaseService<SysRoleModules> getRoleService() {
		return roleService;
	}

	public void setRoleService(IBaseService<SysRoleModules> roleService) {
		this.roleService = roleService;
	}

	/**
	 * 构造方法
	 */
	public OperationAction() {
		// 初始化Map对象
		dataMap = new HashMap<String, Object>();
	}

    public String getEntity() {
		try {
			SysOperation entity = myService.findById(operation.getOperationId());
			BeanUtils.copyProperties(entity, operation);
		} catch (Exception re) {
			
		}
		// 返回结果
		return SUCCESS;
	}

    public String getGridData() {
		// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
		dataMap.clear();
		// 获取数据
		try {
			int page = start == 0 ? 1 : start / length + 1;
			int size = length < 0 ? Integer.MAX_VALUE : length;
			Pager<SysOperation> list = myService.findPagerList(page, size, "", "");
			dataMap.put("draw", draw);
			dataMap.put("recordsTotal", list.getTotal());
			dataMap.put("recordsFiltered", list.getTotal());
			dataMap.put("data", list.getList());
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

    public String getAllData() {
		// 获取数据
        try {
        	List<OperationForm> data = new ArrayList<OperationForm>();
        	List<SysOperation> list = myService.findAll();
        	for(SysOperation po:list){
        		OperationForm vo = new OperationForm();
        		BeanUtils.copyProperties(po, vo);
        		data.add(vo);
        	}
        	dataList = data;
        } catch (Exception re) {
			
		}
		// 返回结果
		return SUCCESS;
	}
    
    public String getModuleBtn() {
		// 获取数据
        try {
        	String mid = ServletActionContext.getRequest().getParameter("mid");
        	Integer moduleId = Integer.parseInt(mid);
        	String userName = ServletActionContext.getRequest().getRemoteUser();
			SysUsers user = userService.find("userUsername", userName);
			if (user.getUserUsername().equals("admin")) {
				dataList = myService.findAll();
			}else{
	        	Set<SysRoles> roles = user.getSysRoleses();
				Set<SysOperation> btns = new HashSet<SysOperation>(0);
				for (SysRoles ro : roles) {
					List<SysRoleModules> roleModule = roleService.findList("sysRoles", ro);
					for (SysRoleModules rm : roleModule) {
						if(rm.getSysModules().getModuleId()==moduleId){
							btns.add(rm.getSysOperation());
						}
					}
				}
	        	dataList = new ArrayList<SysOperation>(btns);
			}
        } catch (Exception re) {
			
		}
		// 返回结果
		return SUCCESS;
	}

	public String addEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysOperation entity = new SysOperation();
		BeanUtils.copyProperties(operation, entity);
		try {
			myService.save(entity);
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public String editEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysOperation entity = myService.findById(operation.getOperationId());
        entity.setOperationName(operation.getOperationName());
        entity.setOperationCode(operation.getOperationCode());
        entity.setOperationClass(operation.getOperationClass());
        entity.setOperationRemark(operation.getOperationRemark());
        entity.setOperationOrder(operation.getOperationOrder());
		try {
			myService.update(entity);
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public String delEntity() {
		// 清空dataMap中的数据
        dataMap.clear();
		try {
			myService.delete(operation.getOperationId());
			dataMap.put("valid", true);
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}
}
